import ycp
ycp.import_module("UI")
from ycp import UI

ycp.widget_names("Widgets")
from Widgets import *
from EducationGUIHelper import *

class Selector:

  def __init__(self, id, available_label, selected_label):

    self.id = id
    self.available_items = []
    self.selected_items = []
    self.available_label = available_label
    self.selected_label = selected_label

  def set_available_items(self, items):  

    for item in items:
      if item in self.selected_items:
        self.selected_items.remove(item)

    self.available_items = items
    self.update_widgets()


  def set_selected_items(self, items):

    for item in self.selected_items:
      self.available_items.append(item)
      
    for item in items:
      if item in self.available_items:
        self.available_items.remove(item)

    self.selected_items = items
    self.update_widgets()

  def update_widgets(self):

    self.available_items.sort()
    self.selected_items.sort()
    
    UI.ChangeWidget(id(self.id + '_available'), sym('Items'), self.available_items)
    UI.ChangeWidget(id(self.id + '_selected'), sym('Items'), self.selected_items)

  def remove(self):

    selection = UI.QueryWidget(id(self.id + '_selected'), sym('CurrentItem'))

    if (not selection):
      return None

    index = self.selected_items.index(selection)
    
    self.available_items.append(selection)
    self.selected_items.remove(selection)
    self.update_widgets()

    if (len(self.selected_items) <= index):
      index = len(self.selected_items) - 1

    if (index >= 0):
      UI.ChangeWidget(id(self.id + '_selected'), sym('CurrentItem'), self.selected_items[index])

    return selection

  def add(self):
    selection = UI.QueryWidget(id(self.id + '_available'), sym('CurrentItem'))

    if (not selection):
      return None

    index = self.available_items.index(selection)
    
    self.selected_items.append(selection)
    self.available_items.remove(selection)
    self.update_widgets()

    if (len(self.available_items) <= index):
      index = len(self.available_items) - 1

    if (index >= 0):
      UI.ChangeWidget(id(self.id + '_available'), sym('CurrentItem'), self.available_items[index])

    return selection

  def remove_all(self):

    removed = list(self.selected_items)

    for item in self.selected_items:
      self.available_items.append(item)

    self.selected_items = []
    self.update_widgets()

    return removed

  def add_all(self):

    added = list(self.available_items)

    for item in self.available_items:
      self.selected_items.append(item)
    
    self.available_items = []    
    self.update_widgets()

    return added

  def get_term(self):

    if UI.TextMode():
      remove_label = " <  "
      add_label = " >  "
      remove_all_label = " << "
      add_all_label = " >> "
    else:
      remove_label = " < "
      add_label = " > "
      remove_all_label = "<<"
      add_all_label = ">>"

    term = \
      HBox(
        SelectionBox(id(self.id + '_available'), self.available_label, self.available_items),
        VBox(
          PushButton(id(self.id + '_remove_all'), opt('key_F4'), remove_all_label),
          PushButton(id(self.id + '_remove'), opt('key_F5'), remove_label),
          PushButton(id(self.id + '_add'), opt('key_F4'), add_label),
          PushButton(id(self.id + '_add_all'), opt('key_F5'), add_all_label),
        ),
        SelectionBox(id(self.id + '_selected'), self.selected_label, self.selected_items)
      )    

    return term